﻿using Microsoft.WindowsAzure;
using System;
using Nardax.Azure.StorageClient;

namespace Nardax.Azure
{
    public class CloudStorageFactory : ICloudStorageFactory
    {
        public ICloudStorageAccount CreateStorageAccount(string connectionString)
        {
            return new CloudStorageAccountWrapper(CloudStorageAccount.Parse(connectionString));
        }

        public ICloudStorageAccount CreateStorageAccount(StorageCredentialsAccountAndKey storageCredentialsAccountAndKey, bool useHttps)
        {
            return new CloudStorageAccountWrapper(new CloudStorageAccount(storageCredentialsAccountAndKey, useHttps));
        }

        public ICloudStorageAccount CreateStorageAccount(StorageCredentials storageCredentials, Uri blobEndpoint, Uri queueEndpoint, Uri tableEndpoint)
        {
            return new CloudStorageAccountWrapper(new CloudStorageAccount(storageCredentials, blobEndpoint, queueEndpoint, tableEndpoint));
        }

        public ICloudQueue GetQueueReference(string queueAddress, string connectionString)
        {
            var account = CreateStorageAccount(connectionString);

            var queueClient = account.CreateCloudQueueClient();

            return queueClient.GetQueueReference(queueAddress);
        }

        public ICloudQueue<T> GetQueueReferenceWithBinarySerializer<T>(string queueAddress, string connectionString)
        {
            var account = CreateStorageAccount(connectionString);

            var queueClient = account.CreateCloudQueueClient();

            return queueClient.GetQueueReferenceWithBinarySerializer<T>(queueAddress);
        }

        public ICloudQueue<T> GetQueueReferenceWithXmlSerializer<T>(string queueAddress, string connectionString)
        {
            var account = CreateStorageAccount(connectionString);

            var queueClient = account.CreateCloudQueueClient();

            return queueClient.GetQueueReferenceWithXmlSerializer<T>(queueAddress);
        }
    }
}
